package com.ysm.smfu.food;

import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.activity_transition.ActivityTransition;
import com.activity_transition.ExitActivityTransition;
import com.bumptech.glide.Glide;
import com.ysm.smfu.R;
import com.ysm.smfu.activity.BaseActivity;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/3/28.
 */
public class FoodLargeActivity extends BaseActivity {
    @BindView(R.id.viewpager)
    ViewPager viewPager;

    private ExitActivityTransition exitActivityTransition;
    private ArrayList<String> list;
    private int position;
    private Intent intent;
    private ImageView view;

    @Override
    protected void initViews() {
        super.initViews();
        setContentView(R.layout.viewpager);
        ButterKnife.bind(this);
        intent = getIntent();
        position = intent.getIntExtra("position", 0);
        list = intent.getStringArrayListExtra("list");
        initViewPager();
    }

    /**
     * 初始化数据
     */
    private void initViewPager() {
        viewPager.setAdapter(new MyAdapter());
        viewPager.setCurrentItem(position);
        exitActivityTransition = ActivityTransition.with(intent).to(viewPager)
                .start(savedInstanceState);
    }

    private class MyAdapter extends PagerAdapter {
        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            view = (ImageView) LayoutInflater.from(FoodLargeActivity.this).inflate(R.layout.imageview, null);
            container.addView(view);
            Glide.with(FoodLargeActivity.this).load(list.get(position))
                    .placeholder(R.drawable.pay_businessicon_defualt)
                    .error(R.drawable.pay_businessicon_defualt)
                    .into(view);
            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(viewPager);
        }
    }

    @Override
    public void onBackPressed() {
        if (exitActivityTransition != null)
            exitActivityTransition.exit(this);
    }
}